<?php


namespace app\index\controller;


use app\BaseController;
use app\index\business\memberBus;
use app\index\validate\memberInfo;
use app\index\validate\memberSelect;
use think\exception\ValidateException;
use think\facade\View;

class Member extends BaseController
{
    public function index(){
        View::assign('title','登录页面');
        return View::fetch();
    }
    public function addMember(){
//        $captcha = $this->request->param('captcha');
        $phone = $this->request->param('phone');
        $code = $this->request->param('code');
        $password = $this->request->param('password');
        $data = [
            'phone' => $phone,
            'code' => $code,
            'password' => $password,
//            'captcha' => $captcha,
        ];
        try {
            validate(memberInfo::class)->scene('member_add')->check($data);
        }catch (ValidateException $e){
            return show(config('status.error'),$e->getError());
        }
//        if(!captcha_check($captcha)){
//            return show(config('status.error'),'验证码不正确');
//        }
        $memberBus = new memberBus();
        try {
            $res = $memberBus->addMember($data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'注册成功',$res);
    }
    public function resetPassword(){
        $password = $this->request->param('password');
        $newPassword = $this->request->param('newPassword');
        $anewPassword = $this->request->param('anewPassword');
        $member_id = $this->request->param('member_id');
        $validateData = [
            'password' => $password,
            'newPassword' => $newPassword,
            'anewPassword' => $anewPassword
        ];
        try {
            validate(memberSelect::class)->scene('change_password')->check($validateData);
        }catch (ValidateException $e){
            return show(config('status.error'),$e->getError());
        }
        try {
            $memberBus = new memberBus();
            $result = $memberBus->resetPasswordBus($member_id,$password,$newPassword);
        } catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'修改成功',$result);
    }
    public function loginCheck(){
        $phone =$this->request->param('phone','');
        $password = $this->request->param('password','');
        $data = [
            'phone' => $phone,
            'password' => $password,
        ];
        try {
            validate(memberSelect::class)->scene('member_login')->check($data);
        }catch (ValidateException $e){
            return show(config('status.error'),$e->getError());
        }
        try {
            $memberBus = new memberBus();
            $result = $memberBus->loginBus($data);
        } catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        if($result){
            return show(config('status.success'),'登录成功',$result);
        }
        return show(config('status.error'),'登录失败');
    }
    public function getMemberByPhone(){
        $phone = $this->request->param('phone');
        $data = [
            'phone'=>$phone
        ];
//        if($phone ==''){
//            return show(config('status.error'),'验证码不正确');
//        }
        $memberBus = new memberBus();
        try {
            $res = $memberBus->getMemberWhere($data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'查询成功',$res);
    }

    public function getMemberLogisticsById(){
        $id = $this->request->param('id');
        $memberBus = new memberBus();
        $result = $memberBus->getMemberLogisticsById($id);
        return show(config('status.success'),'查询成功',$result);
    }
    public function getMemberConsignorById(){
        $id = $this->request->param('id');
        $memberBus = new memberBus();
        $result = $memberBus->getMemberConsignorById($id);
        return show(config('status.success'),'查询成功',$result);
    }
    public function loginSuccess(){
        View::assign('title','等待审核');
        return View::fetch();
    }
    public function login(){
        View::assign('title','登录');
        return View::fetch();
    }
    public function memberInfo(){
        View::assign('title','货主企业认证');
        return View::fetch();
    }

    /**
     * 货主企业添加
     * @return \think\response\Json
     */
    public function addMemberInfo(){
        $memberInfo = $this->request->param('memberInfo');
        $supplement = $this->request->param('supplement');
        $phone = $this->request->param("phone");
        $validate = new memberInfo();
        if(!$validate->scene('memberInfo_add')->check($memberInfo)){
            return show(config('status.error'),$validate->getError());
        }
        if(!$validate->scene('consignor_add')->check($supplement)){
            return show(config('status.error'),$validate->getError());
        }
        $memberBus = new memberBus();
        $result = $memberBus->addMemberInfo($phone,$memberInfo,$supplement);

        return show(config('status.success'),'提交数据成功');
    }
    public function logisticsInfo(){
        View::assign('title','物流企业认证');
        return View::fetch();
    }
    public function addLogisticsInfoInfo(){
        $memberInfo = $this->request->param('memberInfo');
        $supplement = $this->request->param('supplement');
        $phone = $this->request->param("phone");
        $validate = new memberInfo();
        if(!$validate->scene('logisticsInfo_add')->check($memberInfo)){
            return show(config('status.error'),$validate->getError());
        }
//        dump($supplement);exit();
        if(!$validate->scene('logistics_add')->check($supplement)){
            return show(config('status.error'),$validate->getError());
        }
        $memberBus = new memberBus();
        $result = $memberBus->addLogisticsInfo($phone,$memberInfo,$supplement);

        return show(config('status.success'),'提交数据成功',$result);
    }
    public function auditLoading(){
        View::assign('title','等待审核');
        return View::fetch();
    }
    /**会员退出
     * @return \think\response\Redirect
     */
    public function memberLogout(){
        session(config('index.session_member'),null);
        return show(config('status.success'),'退出成功');
    }
    public function changePassword(){
        View::assign('title','密码重置');
       return View::fetch();
    }
    public function changePasswordCheck(){
        $phone = $this->request->param('phone');
        $code = $this->request->param('code');
        $validateData = [
            'phone_set' => $phone,
            'code' => $code,
        ];
        try {
            validate(memberInfo::class)->scene('member_change_check')->check($validateData);
        }catch (ValidateException $e){
            return show(config('status.error'),$e->getError());
        }
        $data = [
            'phone' => $phone,
            'code' => $code,
        ];
        $memberBus = new memberBus();
        try {
            $res = $memberBus->changePasswordCheckBus($data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'提交数据成功',$res);;
    }
    public function setNewPassword(){
        View::assign('title','设置新密码');
        return View::fetch();
    }
    public function updatePassword(){
        $newPassword = $this->request->param('newPassword');
        $anewPassword = $this->request->param('anewPassword');
        if($newPassword != $anewPassword){
            return show(config('status.error'),'两次密码不一致，请重新输入');
        }
        $data = [
            'password' => $newPassword
        ];
        $memberBus = new memberBus();
        try {
            $res = $memberBus->updatePasswordBus($data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'密码更改成功',$res);
    }
}